package com.movie.controller;

import com.movie.dto.ApiResponse;
import com.movie.dto.MovieDTO;
import com.movie.dto.PageResponse;
import com.movie.service.MovieService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/movies")
@RequiredArgsConstructor
@Tag(name = "影片管理", description = "影片信息的增删改查接口")
public class MovieController {
    
    private final MovieService movieService;
    
    @GetMapping
    @Operation(summary = "分页获取影片列表", description = "支持分页、搜索、分类筛选、状态筛选、年份筛选、评分筛选的影片列表查询")
    public ApiResponse<PageResponse<MovieDTO>> getMovies(
            @Parameter(description = "页码，从0开始") @RequestParam(defaultValue = "0") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String search,
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "状态筛选") @RequestParam(required = false) String status,
            @Parameter(description = "最小年份") @RequestParam(required = false) Integer yearMin,
            @Parameter(description = "最大年份") @RequestParam(required = false) Integer yearMax,
            @Parameter(description = "最小评分") @RequestParam(required = false) Double ratingMin,
            @Parameter(description = "最大评分") @RequestParam(required = false) Double ratingMax,
            @Parameter(description = "排序字段") @RequestParam(required = false) String sortBy,
            @Parameter(description = "排序方向") @RequestParam(required = false) String sortDir) {
        Pageable pageable = PageRequest.of(page, size);
        Page<MovieDTO> movies = movieService.findMoviesWithFilters(
            search, categoryId, status, yearMin, yearMax, ratingMin, ratingMax, sortBy, sortDir, pageable);
        
        return ApiResponse.success(PageResponse.of(movies));
    }
    
    @GetMapping("/all")
    @Operation(summary = "获取所有影片", description = "获取系统中所有的影片列表")
    public ApiResponse<List<MovieDTO>> getAllMovies() {
        List<MovieDTO> movies = movieService.findAll(Pageable.unpaged()).getContent();
        return ApiResponse.success(movies);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取影片", description = "根据影片ID获取详细的影片信息")
    public ApiResponse<MovieDTO> getMovieById(
            @Parameter(description = "影片ID") @PathVariable Long id) {
        MovieDTO movie = movieService.findById(id);
        return ApiResponse.success(movie);
    }
    
    @PostMapping
    @Operation(summary = "创建影片", description = "创建新的影片信息")
    public ApiResponse<MovieDTO> createMovie(
            @Parameter(description = "影片信息") @RequestBody MovieDTO movieDTO) {
        MovieDTO created = movieService.create(movieDTO);
        return ApiResponse.success("影片创建成功", created);
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新影片", description = "根据ID更新影片信息")
    public ApiResponse<MovieDTO> updateMovie(
            @Parameter(description = "影片ID") @PathVariable Long id,
            @Parameter(description = "影片信息") @RequestBody MovieDTO movieDTO) {
        MovieDTO updated = movieService.update(id, movieDTO);
        return ApiResponse.success("影片更新成功", updated);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除影片", description = "根据ID删除影片及其相关数据")
    public ApiResponse<Void> deleteMovie(
            @Parameter(description = "影片ID") @PathVariable Long id) {
        movieService.delete(id);
        return ApiResponse.success("影片删除成功", null);
    }
    
    @GetMapping("/category/{categoryId}")
    @Operation(summary = "根据分类获取影片", description = "根据分类ID获取该分类下的影片列表")
    public ApiResponse<List<MovieDTO>> getMoviesByCategory(
            @Parameter(description = "分类ID") @PathVariable Long categoryId) {
        List<MovieDTO> movies = movieService.findByCategory(categoryId, Pageable.unpaged()).getContent();
        return ApiResponse.success(movies);
    }
    
    @GetMapping("/search")
    @Operation(summary = "搜索影片", description = "根据关键词搜索影片")
    public ApiResponse<List<MovieDTO>> searchMovies(
            @Parameter(description = "搜索关键词") @RequestParam String keyword) {
        List<MovieDTO> movies = movieService.search(keyword, Pageable.unpaged()).getContent();
        return ApiResponse.success(movies);
    }
}